package com.ruoyi.web.controller.noUse;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.AESUtils;
import com.ruoyi.student.domain.StudentBaseInfo;
import com.ruoyi.student.service.IStudentBaseInfoService;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController("NoUserController")
@RequestMapping("/no/use")
public class NoUseController extends BaseController {


    @Resource
    private IStudentBaseInfoService studentBaseInfoService;


    @Resource
    private Environment environment;


    /**
     * 加密学生身份证号
     * 测试没有问题
     *
     * @return
     */
    @PostMapping("/encryptStudentIdNumber")
    public AjaxResult encryptStudentIdNumber() throws Exception {

        //如果当前是生产环境，那么不进行处理
        String devActive = environment.getProperty("spring.profiles.active");
        if (devActive.equals("druid")) {
            return success();
        }

        List<StudentBaseInfo> query = studentBaseInfoService.selectStudentBaseInfoList(null);
        for (StudentBaseInfo item : query) {
            LambdaUpdateWrapper<StudentBaseInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(StudentBaseInfo::getCode, item.getCode());
            lambdaUpdateWrapper.set(StudentBaseInfo::getIdNumber, AESUtils.AESEnCode(item.getIdNumber()));
            studentBaseInfoService.update(lambdaUpdateWrapper);
        }
        return success();
    }


    /**
     * 解密学生身份证号
     * 测试没有问题
     *
     * @return
     */
    @PostMapping("/decryptStudentIdNumber")
    public AjaxResult decryptStudentIdNumber() throws Exception {
        //如果当前是生产环境，那么不进行处理
        String devActive = environment.getProperty("spring.profiles.active");
        if (devActive.equals("druid")) {
            return success();
        }
        List<StudentBaseInfo> query = studentBaseInfoService.selectStudentBaseInfoList(null);
        for (StudentBaseInfo item : query) {
            LambdaUpdateWrapper<StudentBaseInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(StudentBaseInfo::getCode, item.getCode());
            String test = AESUtils.AESDeCode(item.getIdNumber());
            lambdaUpdateWrapper.set(StudentBaseInfo::getIdNumber, AESUtils.AESDeCode(item.getIdNumber()));
            studentBaseInfoService.update(lambdaUpdateWrapper);
        }
        return success();
    }

}
